package com.example.my.meila.view.adapter.beautyadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.my.meila.R;
import com.example.my.meila.common.image.ImageLoader;
import com.example.my.meila.constast.Constast;
import com.example.my.meila.entity.Good;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.support.v7.widget.RecyclerView.ViewHolder;

/**
 * Created by My on 2016/10/31.
 */
public class GoodAdapter extends RecyclerView.Adapter<ViewHolder> {



    private Context context;
    private List<Good.DataBean.FeedsBean> data;

    private static final int TYPE_CONT = 0;
    private static final int TYPE_DEEP = 1;
    private static final int TYPE_AD = 2;


    public GoodAdapter(Context context, List<Good.DataBean.FeedsBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = null;
        switch (getItemViewType(viewType)) {
            case 0:
                holder = new ContentViewHolder(LayoutInflater.from(context).inflate(R.layout.item_beauty_goodcontent, null));
                break;
            case 1:
                holder = new DeepViewHolder(LayoutInflater.from(context).inflate(R.layout.item_beaut_good2, null));
                break;
            case 2:
                holder = new AdViewHolder(LayoutInflater.from(context).inflate(R.layout.item_good_ad, null));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (holder instanceof ContentViewHolder) {
            if (null != data.get(position).getUser()) {
                ImageLoader.getInstance().transformationsImage(context, Constast.PUBLIC_URL + data.get(position).getUser().getAvatar(), ((ContentViewHolder) holder).goodIv1);
                ImageLoader.getInstance().display(context, Constast.PUBLIC_URL + data.get(position).getUser().getNew_type_icon(), ((ContentViewHolder) holder).goodIv2);
                ((ContentViewHolder) holder).goodTv1.setText(data.get(position).getUser().getNickname());
            } else {
                ((ContentViewHolder) holder).goodRl.setVisibility(View.GONE);

            }



            ImageLoader.getInstance().display(context, Constast.PUBLIC_URL + data.get(position).getFeed().getImg(), ((ContentViewHolder) holder).goodIv3);
            ((ContentViewHolder) holder).goodTv3.setText(data.get(position).getFeed().getSummary());
            ((ContentViewHolder) holder).goodTv4.setText(data.get(position).getLike_count() + "");
            ((ContentViewHolder) holder).goodTv5.setText(data.get(position).getParticipate_count() + "");


        }
        if (holder instanceof DeepViewHolder) {
            ((DeepViewHolder) holder).good2Tv1.setText(data.get(position).getFeed().getTitle());

        }

        if (holder instanceof AdViewHolder) {
            ImageLoader.getInstance().display(context, Constast.PUBLIC_URL + data.get(position).getUser().getAvatar(), ((AdViewHolder) holder).goodAdIv1);
            ((AdViewHolder) holder).goodAdTv1.setText(data.get(position).getUser().getNickname());
            ((AdViewHolder) holder).goodAdTv3.setText("唯老大帅哥");

        }

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        int item = 0;
        switch (data.get(position).getType()) {
            case "feed":
                item = TYPE_CONT;
                break;
            case "date_feed":
                item = TYPE_DEEP;
                break;
            case "ad_position":
                item = TYPE_AD;
                break;
        }
        return item;
    }

    class ContentViewHolder extends ViewHolder {

        @BindView(R.id.good_iv1)
        ImageView goodIv1;
        @BindView(R.id.good_iv2)
        ImageView goodIv2;
        @BindView(R.id.good_tv1)
        TextView goodTv1;
        @BindView(R.id.good_tv2)
        TextView goodTv2;
        @BindView(R.id.good_rl)
        RelativeLayout goodRl;
        @BindView(R.id.good_iv3)
        ImageView goodIv3;
        @BindView(R.id.good_tv3)
        TextView goodTv3;
        @BindView(R.id.good_tv4)
        TextView goodTv4;
        @BindView(R.id.good_tv5)
        TextView goodTv5;

        public ContentViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class DeepViewHolder extends ViewHolder {
        @BindView(R.id.good2_tv1)
        TextView good2Tv1;

        public DeepViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class AdViewHolder extends ViewHolder {
        @BindView(R.id.good_ad_iv1)
        ImageView goodAdIv1;
        @BindView(R.id.good_ad_tv1)
        TextView goodAdTv1;
        @BindView(R.id.good_ad_tv2)
        TextView goodAdTv2;
        @BindView(R.id.good_ad_rl)
        RelativeLayout goodAdRl;
        @BindView(R.id.good_ad_iv2)
        ImageView goodAdIv2;
        @BindView(R.id.good_ad_tv3)
        TextView goodAdTv3;

        public AdViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
